package cn.unknowpupil.init.common.resopnse;

import cn.unknowpupil.init.common.enums.ResultStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description: <p>
 * 通用响应结果
 * </p>
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2020/5/28 13:23
 * @Classname: ResultResponse
 * @Version: 1.0
 */
@Data
public class ResultResponse<T> implements Serializable {
    private static final long serialVersionUID = -2730911495798606615L;

    private boolean success;

    private int code;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String path;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String throwable;

    private Long timestamp;
    /**
     * 功能描述: <br>
     * 〈 默认成功 〉
     *
     * @param <T> {@link T}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 13:59
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildSuccess() {
        return new ResultResponse<T>(true, ResultStatus.SUCCESS.code(), ResultStatus.SUCCESS.message());
    }

    /**
     * 功能描述: <br>
     * 〈 返回成功参数 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 14:01
     * @since: 1.0.0
     * @Param: data {@link T}  成功数据
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildSuccess(T data) {
        return new ResultResponse<T>(true, ResultStatus.SUCCESS.code(),data, ResultStatus.SUCCESS.message());
    }
    /**
     * 功能描述: <br>
     * <p>
     *   〈  自定义信息 〉
     *</p>
     *
     * @auther: w10g
     * @Date: 2020/6/5 12:14
     * @since: 1.0.0
     * @param message {@code String}  信息
     * @return: {@link ResultResponse <T>}
     * @return:
     */
    public static <T> ResultResponse<T> success(String message) {
        return new ResultResponse<T>(true, ResultStatus.SUCCESS.code(), message);
    }

    /**
     * 功能描述: <br>
     * 〈  自定义状态信息 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 14:01
     * @since: 1.0.0
     * @Param: enums {@link ResultStatus}  自定义成功状态信息
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildSuccess(ResultStatus enums) {
        return new ResultResponse<T>(true,enums.code(),enums.message());
    }


    /**
     * 功能描述: <br>
     * 〈 返回成功参数 && 自定义状态信息 〉
     *
     * @auther: w10g
     * @Date: 2020/1/16 14:01
     * @since: 1.0.0
     * @Param: enums {@link ResultStatus}  自定义成功状态信息
     * @Param: data {@link T}  成功数据
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildSuccess(ResultStatus enums, T data) {
        return new ResultResponse<T>(true,enums.code(),data,enums.message());
    }


    /**
     * 功能描述: <br>
     * 〈 返回默认错误信息 〉
     *
     * @param <T> {@link <T>}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:09
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure() {
        return new ResultResponse<T>(false, ResultStatus.FAILURE.code(), ResultStatus.FAILURE.message());
    }

    /**
     * 功能描述: <br>
     * 〈 返回默认错误信息 〉
     *
     * @param enums {@link ResultStatus}  泛型
     * @param path {@code String}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:09
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure(ResultStatus enums, String path) {
        return new ResultResponse<T>(false,enums,path);
    }

    /**
     * 功能描述: <br>
     * 〈 返回默认错误信息 〉
     *
     * @param code {@link ResultStatus}  泛型
     * @param path {@code String}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:09
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure(Integer code,String message, String path) {
        return new ResultResponse<T>(false,code,message,path);
    }

    /**
     * 功能描述: <br>
     * 〈 返回throwable错误信息 〉
     *
     * @param enums {@link ResultStatus}  泛型
     * @param throwable {@code String}  泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:09
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure(ResultStatus enums, Throwable throwable) {
        return new ResultResponse<T>(false,enums,throwable);
    }

    /**
     * 功能描述: <br>
     * 〈 〉
     *
     * @param enums {@link ResultStatus} 自定义错误状态
     * @param <T>   {@link T} 泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:11
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure(ResultStatus enums) {
        return new ResultResponse<T>(false,enums.code(),enums.message());
    }

    /**
     * 功能描述: <br>
     * 〈 系统异常信息 〉
     *
     * @param code    {@code Integer} 系统错误码
     * @param message {@code String} 系统错误信息
     * @param <T>     {@link T} 泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:11
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure(Integer code, String message) {
        return new ResultResponse<>(false,code,message);
    }
    /**
     * 功能描述: <br>
     * 〈 系统异常信息 〉
     *
     * @param message {@code String} 系统错误信息
     * @param <T>     {@link T} 泛型
     * @auther: w10g
     * @Date: 2020/1/16 14:11
     * @since: 1.0.0
     * @return: {@link ResultResponse <T>}
     */
    public static <T> ResultResponse<T> buildFailure(String message) {
        return new ResultResponse<T>(false, ResultStatus.FAILURE.code(),message);
    }


    public static <T> ResultResponse<T> buildSuccess(int result) {
        if(result>0){
            return success("操作成功");
        }else {
            return buildFailure("操作失败");
        }
    }
    public static <T> ResultResponse<T> buildSuccess(boolean result) {
        if(result){
            return success("操作成功");
        }else {
            return buildFailure("操作失败");
        }
    }

    //======================================构造器================================

    public ResultResponse() {
        super();
    }
    public ResultResponse(T data) {
        this(true, ResultStatus.SUCCESS.code(), data,"success");
    }

    public ResultResponse(Boolean success, Integer code, String message) {
        super();
        this.success = success;
        this.code = code;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }



    public ResultResponse(Boolean success, Integer code, T data, String message) {
        super();
        this.success = success;
        this.code = code;
        this.data = data;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }

    public ResultResponse(Boolean success, Integer code, String message, String path) {
        super();
        this.success = success;
        this.code = code;
        this.path = path;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }

    public ResultResponse(Boolean success, ResultStatus enums, String path) {
        super();
        this.success = success;
        this.code = enums.code();
        this.message = enums.message();
        this.path = path;
        this.timestamp = System.currentTimeMillis();
    }
    public ResultResponse(Boolean success, ResultStatus enums, Throwable throwable) {
        super();
        this.success = success;
        this.code = enums.code();
        this.message = enums.message();
        this.throwable = throwable.getMessage();
        this.timestamp = System.currentTimeMillis();
    }
}
